// created by noellynch
// Jun 8, 2009

package com.sceneworker.monitorpopup.panels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;

import com.jme.scene.state.BlendState;
import com.myjavautils.swing.DrawUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;

public class BlendCalculatorPanel extends JPanel {

	private		JColorChooser		mcl_src;
	private		JColorChooser		mcl_dst;
	private		Preview				mcl_dstFactorPreview;
	private		Preview				mcl_srcFactorPreview;
	private		Preview				mcl_outputPreview;
	private		JComboBox			mcl_dstCombo;
	private		JComboBox			mcl_srcCombo;
	
	public BlendCalculatorPanel(JDialog cl_dlg) {
		setLayout(new MigLayout("inset 5"));
		
		MigLayoutUtils.makeLabelAndSeperator(this, "Simple Calculator");
		
		makeCalcPanel();
		
		MigLayoutUtils.makeLabelAndSeperator(this, "Colour Calculator");
		
		// src panel
		JPanel		lcl_srcPanel = MigLayoutUtils.makeStandardPanel();
		JLabel		lcl_lbl = SwingComponentFactory.makeALabel("Source Colour");
		mcl_src = SwingComponentFactory.makeRGBOnlyColourChooser();
		Preview		lcl_srcPreview = new Preview(mcl_src);
		mcl_src.setPreviewPanel(lcl_srcPreview);
		mcl_src.setFont(SceneWorkerGlobals.SMALL_FONT);
		
		lcl_srcPanel.add(lcl_lbl, "wrap");
		lcl_srcPanel.add(mcl_src, "width 100%, wrap");
		lcl_srcPanel.add(lcl_srcPreview, "width 100%");
		add(lcl_srcPanel, "width 50%");
		
		// dst panel
		JPanel		lcl_dstPanel = MigLayoutUtils.makeStandardPanel();
		JLabel		lcl_lbl2 = SwingComponentFactory.makeALabel("Destination Colour");
		mcl_dst = SwingComponentFactory.makeRGBOnlyColourChooser();
		Preview		lcl_dstPreview = new Preview(mcl_dst);
		mcl_dst.setPreviewPanel(new Preview(mcl_dst));
		mcl_dst.setFont(SceneWorkerGlobals.SMALL_FONT);
		lcl_dstPanel.add(lcl_lbl2, "wrap");
		lcl_dstPanel.add(mcl_dst, "width 100%, wrap");
		lcl_dstPanel.add(lcl_dstPreview, "width 100%");
		add(lcl_dstPanel, "width 50%, wrap");
		
		makeSourceCombo();
		makeDestinationCombo();
		
		mcl_src.getSelectionModel().addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				updateDstFactor(mcl_dstCombo);
				updateOutput();
				repaint();
			}
		});
		
		mcl_dst.getSelectionModel().addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				updateSrcFactor(mcl_srcCombo);
				updateOutput();
				repaint();
			}
		});
		
		JLabel		lcl_output = SwingComponentFactory.makeALabel("Output");
		add(lcl_output, "wrap");
		mcl_outputPreview = new Preview();
		add(mcl_outputPreview, "width 100%, growx, span");
		
	}	
	
	private		void	makeCalcPanel() {
		JPanel		lcl_p = MigLayoutUtils.makeStandardPanel();
		
		final	JLabel			lcl_v0 = SwingComponentFactory.makeALabel("v");
		JLabel			lcl_v = SwingComponentFactory.makeALabel(" = (SrcColour");
		final	JTextField		lcl_srcC = SwingComponentFactory.makeANumberTextField(0.0f);
		JLabel			lcl_v2 = SwingComponentFactory.makeALabel("* SrcFactor");
		final	JTextField		lcl_srcF = SwingComponentFactory.makeANumberTextField(0.0f);
		JLabel			lcl_v3 = SwingComponentFactory.makeALabel(") + (DstColour");
		final	JTextField		lcl_dstC = SwingComponentFactory.makeANumberTextField(0.0f);
		JLabel			lcl_v4 = SwingComponentFactory.makeALabel("* DstFactor");
		final	JTextField		lcl_dstF = SwingComponentFactory.makeANumberTextField(0.0f);
		JButton		lcl_calc = SwingComponentFactory.makeAButton("=");
		lcl_calc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				float		lf_src = Float.parseFloat(lcl_srcC.getText());
				float		lf_srcF = Float.parseFloat(lcl_srcF.getText());
				float		lf_dst = Float.parseFloat(lcl_dstC.getText());
				float		lf_dstF = Float.parseFloat(lcl_dstF.getText());
				
				float		lf_v = (lf_src * lf_srcF) + (lf_dst * lf_dstF);
				lcl_v0.setText("" + lf_v);
				repaint();
			}
		});
		
		
		lcl_p.add(lcl_v0);
		lcl_p.add(lcl_v);
		lcl_p.add(lcl_srcC);
		lcl_p.add(lcl_v2);
		lcl_p.add(lcl_srcF);
		lcl_p.add(lcl_v3);
		lcl_p.add(lcl_dstC);
		lcl_p.add(lcl_v4);
		lcl_p.add(lcl_dstF);
		lcl_p.add(lcl_calc);
		
		add(lcl_p, "wrap, width 100%, span, growx");
	}
	
	private	void	makeSourceCombo() {
		JLabel			lcl_lbl3 = SwingComponentFactory.makeALabel("Src Func");
		
		DefaultComboBoxModel		lcl_model = new DefaultComboBoxModel();
		
		lcl_model.addElement(BlendState.SourceFunction.DestinationColor);
		lcl_model.addElement(BlendState.SourceFunction.OneMinusDestinationColor);
		lcl_model.addElement(BlendState.SourceFunction.One);
		lcl_model.addElement(BlendState.SourceFunction.Zero);
		
		mcl_srcCombo = SwingComponentFactory.makeAComboBox();
		mcl_srcCombo.setModel(lcl_model);
		mcl_srcCombo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateSrcFactor(mcl_srcCombo);
				updateOutput();
				repaint();
			}
		});
		JPanel			lcl_p1 = MigLayoutUtils.makeStandardPanel();
		
		mcl_srcFactorPreview = new Preview();
		
		lcl_p1.add(lcl_lbl3);
		lcl_p1.add(mcl_srcCombo, "wrap");
		lcl_p1.add(mcl_srcFactorPreview, "span, growx");
		add(lcl_p1, "width 50%");
	}
	
	private	void	makeDestinationCombo() {
		JLabel			lcl_lbl3 = SwingComponentFactory.makeALabel("Dst Func");
		
		DefaultComboBoxModel		lcl_model = new DefaultComboBoxModel();
		
		lcl_model.addElement(BlendState.DestinationFunction.SourceColor);
		lcl_model.addElement(BlendState.DestinationFunction.OneMinusSourceColor);
		lcl_model.addElement(BlendState.DestinationFunction.One);
		lcl_model.addElement(BlendState.DestinationFunction.Zero);
		
		mcl_dstCombo = SwingComponentFactory.makeAComboBox();
		mcl_dstCombo.setModel(lcl_model);
		mcl_dstCombo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateDstFactor(mcl_dstCombo);
				updateOutput();
				repaint();
			}
		});
		JPanel			lcl_p1 = MigLayoutUtils.makeStandardPanel();
		
		mcl_dstFactorPreview = new Preview();
		
		lcl_p1.add(lcl_lbl3);
		lcl_p1.add(mcl_dstCombo, "wrap");
		lcl_p1.add(mcl_dstFactorPreview, "span, growx");
		add(lcl_p1, "width 50%, wrap");
	}
	
	private	void	updateOutput() {
		Color		lcl_src = mcl_src.getColor();
		Color		lcl_srcFactor = mcl_srcFactorPreview.mcl_colour;
		Color		lcl_dst = mcl_dst.getColor();
		Color		lcl_dstFactor = mcl_dstFactorPreview.mcl_colour;
		
		float		lf_r = makeColorComponent(lcl_src.getRed(), lcl_srcFactor.getRed(), lcl_dst.getRed(), lcl_dstFactor.getRed());
		float		lf_g = makeColorComponent(lcl_src.getGreen(), lcl_srcFactor.getGreen(), lcl_dst.getGreen(), lcl_dstFactor.getGreen());
		float		lf_b = makeColorComponent(lcl_src.getBlue(), lcl_srcFactor.getBlue(), lcl_dst.getBlue(), lcl_dstFactor.getBlue());
		
		//System.out.println(String.format("out = %.2f, %.2f, %.2f", lf_r, lf_g, lf_b));
		mcl_outputPreview.mcl_colour = new Color(lf_r, lf_g, lf_b);
	}
	
	private		float		makeColorComponent(int i_clr, int i_factor, int i_clr2, int i_factor2) {
		float		lf_result = ((float)i_factor)/255f;
		lf_result = (i_clr * lf_result) / 255f;
		
		float		lf_result2 = ((float)i_factor2)/255f;
		lf_result2 = (i_clr2 * lf_result2) / 255f;
		
		//System.out.println("SRC " + lf_result + " DST " + lf_result2);
		
		lf_result += lf_result2;
		
		if(lf_result > 1.0f) {
			lf_result = 1.0f;
		} else if(lf_result < 0.0f) {
			lf_result = 0.0f;
		}
		
		return lf_result;
	}
	
	private	void	updateDstFactor(JComboBox cl_dstFactorCombo) {
		if(cl_dstFactorCombo.getSelectedItem() == BlendState.DestinationFunction.SourceColor) {
			mcl_dstFactorPreview.mcl_colour = mcl_src.getColor();
		} else if(cl_dstFactorCombo.getSelectedItem() == BlendState.DestinationFunction.OneMinusSourceColor) {
			mcl_dstFactorPreview.mcl_colour = new Color(255 - mcl_src.getColor().getRed(), 255 - mcl_src.getColor().getGreen(), 255 - mcl_src.getColor().getBlue());
		} else if(cl_dstFactorCombo.getSelectedItem() == BlendState.DestinationFunction.One) {
			mcl_dstFactorPreview.mcl_colour = Color.white;
		} else {
			mcl_dstFactorPreview.mcl_colour = Color.black;
		}
	}
	
	private	void	updateSrcFactor(JComboBox cl_srcFactorCombo) {
		if(cl_srcFactorCombo.getSelectedItem() == BlendState.SourceFunction.DestinationColor) {
			mcl_srcFactorPreview.mcl_colour = mcl_dst.getColor();
		} else if(cl_srcFactorCombo.getSelectedItem() == BlendState.SourceFunction.OneMinusDestinationColor) {
			mcl_srcFactorPreview.mcl_colour = new Color(255 - mcl_dst.getColor().getRed(), 255 - mcl_dst.getColor().getGreen(), 255 - mcl_dst.getColor().getBlue());
		} else if(cl_srcFactorCombo.getSelectedItem() == BlendState.SourceFunction.One) {
			mcl_srcFactorPreview.mcl_colour = Color.white;
		} else {
			mcl_srcFactorPreview.mcl_colour = Color.black;
		}
	}
	
	class	Preview extends JComponent {

		public		Color		mcl_colour = Color.white;
		
		public	Preview(final JColorChooser cl_color) {
			setLayout(new MigLayout("inset 5"));
			final	Preview		lcl_this = this;
			
			cl_color.getSelectionModel().addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					mcl_colour = cl_color.getColor();
					lcl_this.repaint();
				}
			});
		}
		
		public	Preview() {
			setLayout(new MigLayout("inset 5"));
		}
		
		@Override
		public Dimension getPreferredSize() {
			return new Dimension(100, 30);
		}

		@Override
		protected void paintComponent(Graphics cl_g) {
			cl_g.setColor(mcl_colour);
			cl_g.fillRect(0, 0, getWidth(), getHeight());
			
			cl_g.setColor(Color.black);
			DrawUtils.drawComponentRect(cl_g, this);
		}
		
	}
}
